/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.darly.std.vm;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.darly.chinese.common.SpController;
import com.darly.chinese.controller.fileload.ExternalStorageUtil;
import com.darly.chinese.db.chinese.bean.SongCiAuthorModel;
import com.darly.chinese.db.chinese.bean.SongCiModel;
import com.darly.chinese.db.crud.DataReposController;
import com.darly.dlcommon.app.ActionView;
import com.darly.dlcommon.app.App;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.framework.SpEnum;
import com.darly.std.BR;
import com.darly.std.R;
import com.darly.std.guide.MainGuideComponent;
import com.darly.widget.guideview.Guide;
import com.darly.widget.guideview.GuideBuilder;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Description TODO:ViewModel类
 * Package com.darly.std.vm
 *
 * @author zhangyuhui
 * @date 2019/8/12 16:04
 */
public class MainViewModel extends ViewModel implements OnItemClickListener<String> {

    private Timer timer;
    private int count = 0;


    private MutableLiveData<ActionView> action = new MutableLiveData<>();
    private MutableLiveData<String> image = new MutableLiveData<>();

    public MainViewModel() {
        items.add(new ItemMainViewModel(SongCiAuthorModel.getClassName(), R.mipmap.ic_main_pic_one, this));
        items.add(new ItemMainViewModel(SongCiModel.getClassName(), R.mipmap.ic_main_pic_one, this));
        items.add(new ItemMainViewModel(SongCiAuthorModel.getClassName(), R.mipmap.ic_main_pic_one, this));
        items.add(new ItemMainViewModel(SongCiModel.getClassName(), R.mipmap.ic_main_pic_one, this));
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    count++;
                    if (count >= 100) {
                        count = 0;
                    }
                    action.postValue(ActionWrapper.getInstance().getActionViewTag(count, ActionWrapper.TIME_COUNT));
                }
            }, 0, 100);
        }
        DataReposController.test();
        image.setValue(ExternalStorageUtil.getDownLoadPath() + File.separator + "pic.jpg");
    }

    public ObservableList<ItemMainViewModel> items = new ObservableArrayList<>();
    public ItemBinding<ItemMainViewModel> itemBinding = ItemBinding.of(BR.itemMainViewModel, R.layout.item_main_author);


    public MutableLiveData<ActionView> getAction() {
        return action;
    }

    @Override
    public void onItemClick(final View view, String s) {
        DLog.d("onItemClick", "onItemClick() called with: name = [" + s + "]");
        action.postValue(ActionWrapper.getInstance().getActionView(s, App.NEXT_PAGE));
    }

    public void firstGuideView(View view) {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(view)
                .setAlpha(150)
                .setHighTargetCorner(20)
                .setHighTargetPadding(10)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
                DLog.d("setOnVisibilityChangedListener:onShown");
            }

            @Override
            public void onDismiss() {
                SpController.getInstance().putValue(SpEnum.FIRST_OPEN, true);
                DLog.d("setOnVisibilityChangedListener:onDismiss");
            }
        });

        builder.addComponent(new MainGuideComponent());
        Guide guide = builder.createGuide();
        guide.setShouldCheckLocInWindow(true);
        guide.show((Activity) view.getContext());
    }


    public void updateImage(String path) {
        image.postValue(path);
    }

    /**
     * 1. 带view的参数：@{(view)->mainViewModel.tableClick(mainViewModel.image,view)}
     * 跳转到表格界面
     *
     * @param view 参数
     */
    public void tableClick(String path, View view) {
        DLog.d("tableClick", path + view);
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.TABLE_EDIT));
    }

    /**
     * 2. 不带参数：@{() -> viewModel.click()}
     */
    public void click() {
        DLog.d("click");
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.RN_MAIN_VIEW));
    }


    /**
     * 2. 不带参数：@{() -> viewModel.click()}
     */
    public void visit() {
        DLog.d("click");
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.VISIT_ENTITY));
    }

    /**
     * 2. 不带参数：@{() -> viewModel.click()}
     */
    public void detail() {
        DLog.d("click");
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.DETAIL_ENTITY));
    }


    /**
     * 4. @{viewModel::imageClick}
     * 跳转到图片编辑页面
     *
     * @param view 参数
     */
    public void imageClick(View view) {
        DLog.d("imageClick", view);
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.IMAGE_EDIT));
    }

    /**
     * 5. @{viewModel::collectionClick}
     * 跳转到图片编辑页面
     *
     * @param view 参数
     */
    public void collectionClick(View view) {
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.COLLECTION_CLICK));
    }

    /**
     * 5. @{viewModel::listViewClick}
     * 跳转到图片编辑页面
     *
     * @param view 参数
     */
    public void listViewClick(View view) {
        //尝试网络请求
//        RxjavaRetrofitRequestUtil.getInstance().get(HttpInterface.class).getKey((String) ContextController.getInstance().getSharePerferenceController().getValue(SpEnum.SYSTEM_IP))
//                .subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .map(new Func1<JsonObject, String>() {
//                    @Override
//                    public String call(JsonObject s) {
//                        DLog.json("Func1", s.toString());
//                        return s.toString();
//                    }
//                })
//                .subscribe(new rx.Observer<String>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        DLog.json("onError", e.getMessage());
//                        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.LIST_VIEW_CLICK));
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//                        DLog.json("onNext", s);
//                        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.LIST_VIEW_CLICK));
//                    }
//                });


    }


    public void mainPageClick(View view) {
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.MAIN_PAGE));
    }

    public void mapPageClick(View view) {
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.MAP_PAGE));
    }

    public void settingPageClick(View view) {
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.SETTING_PAGE));
    }

    public void objectListClick(View view) {
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.OBJECT_LIST));
    }

    public void payEntityClick(View view) {
        action.postValue(ActionWrapper.getInstance().getActionViewTag(null, ActionWrapper.PAY_ENTITY));
    }

    public MutableLiveData<String> getImage() {
        return image;
    }

    @BindingAdapter("image")
    public static void image(ImageView view, String img) {
//        Glide.with(view.getContext()).load(img).into(view);
    }
}
